package com.situ.company.department.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONObject;

import com.situ.company.department.model.DepartmentModel;
import com.situ.company.department.service.DepartmentService;
import com.situ.company.util.FmtRequest;
@WebServlet("/department")
public class DepartmentServlet extends HttpServlet{
	
	private static final long serialVersionUID = 1L;
	private DepartmentService service = new DepartmentService();
		
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String action = req.getParameter("action");//获取隐藏域中的action
		String result = "";
		switch (action) {
		case "add":
			result = add(req);
			break;
		case "sel":
			result = sel(req);
			break;
		case "del":
			result = del(req);
			break;
		case "get":
			result = get(req);
			break;
		case "upd":
			result = upd(req);
			break;
		case "upd2":
			upd2(req);
			req.getRequestDispatcher("/web/page/department/upd2.jsp").forward(req, resp);
			return;
		case "list":
			result = list(req);
			break;
		}
		FmtRequest.out(resp, result);	
}

	private String list(HttpServletRequest req) {//分页
		String code = req.getParameter("code");// 获取参数
		String name = req.getParameter("name");
		String pageIndex = req.getParameter("pageIndex");// 获取参数
		String pageLimit = req.getParameter("pageLimit");
		DepartmentModel model = new DepartmentModel(code,name);
		model.setPageIndex(Integer.parseInt(pageIndex));
		model.setPageLimit(Integer.parseInt(pageLimit));
		model.setPageOn(true);
//		List<DepartmentModel> list = service.selectList(model);
//		int count = service.selectCount(model);
		//int count = list.size();//分页后的记录条数==带有查询条件的带有limit的查询结构的记录条数（<=10）
		//分页前的记录条数=带有查询条件的不带有limit的查询结构的记录条数（>=10）
		Map<String,Object> map = new HashMap<>();
		map.put("data", service.selectList(model));
		map.put("count", service.selectCount(model));
		return new JSONObject(map).toString();
	}
	private void upd2(HttpServletRequest req) {
		String code = req.getParameter("code");	
		DepartmentModel model = new DepartmentModel(code);
		DepartmentModel mdb = service.selectModel(model);
		req.setAttribute("mdb", mdb);
	}

	/**
	 * 修改
	 * 1-获取参数
	 * 2-封装对象
	 * 3-调用service方法
	 * @param req
	 * @return
	 */
	private String upd(HttpServletRequest req) {
//		String code = req.getParameter("code");// 获取参数
//		String name = req.getParameter("name");
//		String tel = req.getParameter("tel");
//		DepartmentModel model = new DepartmentModel(code,name,tel);
		
		Map<String, String> fields = new HashMap<>();
		fields.put("code", "code");
		fields.put("name", "name");
		fields.put("tel", "tel");
		DepartmentModel model = FmtRequest.parseModel(req, DepartmentModel.class, fields);
		
		return service.update(model);
	}

	/**
	 * 回显
	 * 修改时把数据显示出来
	 * @param req
	 * @return
	 */
	private String get(HttpServletRequest req) {
		String code = req.getParameter("code");	
		DepartmentModel model = new DepartmentModel(code);
		DepartmentModel mdb = service.selectModel(model);
		return new JSONObject(mdb).toString();
	}

	/**
	 * 获取参数根据code删除
	 * @param req
	 * @return
	 */
	private String del(HttpServletRequest req) {
		String code = req.getParameter("code");	
		DepartmentModel model = new DepartmentModel(code);
		return service.delete(model);
	}

	/**
	 * 查询
	 * @param req
	 * @return
	 */
	private String sel(HttpServletRequest req) {
		String code = req.getParameter("code");// 获取参数
		String name = req.getParameter("name");
		DepartmentModel model = new DepartmentModel(code,name);
		List<DepartmentModel> list = service.selectList(model);
		return new JSONArray(list).toString();//json数组格式转换成字符串
	}

	
	/**
	 * 添加
	 * @param req
	 * @return
	 */
	private String add(HttpServletRequest req) {
		String code = req.getParameter("code");// 获取参数
		String name = req.getParameter("name");
		String tel = req.getParameter("tel");
		DepartmentModel model = new DepartmentModel(code,name,tel);
		
		return service.insert(model);
	}
	
}
